<?php 
namespace App\Models;
use Log;
use App\Cache\BaseCache;

class Favorite extends BaseModel{
    
	//表明
	protected $table = 'favorite';
	//主键
	protected $primaryKey = 'id';
	
    public function addFavorite(){
        $userId = $this->request->input('userId');
        $liveId = $this->request->input('liveId');
        $favorite = $this->select('id','user_id','live_id')
                ->where('user_id','=',$userId)
                ->where('live_id','=',$liveId)
                ->where('is_delete','=',0)
                ->first();
        if($favorite){
            return TRUE;
        }
        $favorite = new Favorite($this->request);
        $favorite->user_id = $userId;
        $favorite->live_id = $liveId;
        $favorite->save();
        return TRUE;
    }
    
    public function delFavorite(){
        $userId = $this->request->input('userId');
        $liveId = $this->request->input('liveId');
        $favorite = $this->select('id','user_id','live_id')
                ->where('user_id','=',$userId)
                ->where('live_id','=',$liveId)
                ->first();
        if($favorite){
            $favorite->is_delete = 1;
            $favorite->save();
        }
        return TRUE;
    }
    
    //我的收藏列表
    public function getFavoriteList(){
        $data = with(new Live($this->request))->getFavoriteList();
        return $data;
    }
    
    //判断是否收藏
    public function isFavorite($userId,$liveId){
        $favorite = $this->select('id','user_id','live_id')
                ->where('user_id','=',$userId)
                ->where('live_id','=',$liveId)
                ->where('is_delete','=','0')
                ->first();
        if(!$favorite){
           $favorite = new Favorite($this->request);
           $favorite->user_id = $userId;
           $favorite->live_id = $liveId;
           $favorite->is_delete = 1;
        }
        $favorite->saveModel();
        return !$favorite->is_delete ;
    }
    
    //更新实体
    public function saveModel(){
        $cache = new BaseCache();
        $cache->method = 'forever';
        $cache->cacheName = 'favorite';
        $cache->cachePar = array($this->user_id,$this->live_id); //集合
        if($this->is_delete == 0){
            $cache->cacheValue = 'FAVORITE';
        } else {
            $cache->method = 'put';
            $cache->cacheValue = 'UNFAVORITE';
        }
        $cache->HandleCache();
       
        //我的收藏更新
        $cache->method = 'forget';
        $cache->cacheName = 'getFavoriteList';
        $cache->cachePar = array($this->user_id); //集合
        $cache->HandleCache();
    }
    
    //删除实体
    public function deleteModel(){
        $cache = new BaseCache();
        $cache->method = 'forever';
        $cache->cacheName = 'favorite';
        $cache->cachePar = array($this->user_id,$this->live_id); //集合
        $cache->cacheValue = 'UNFAVORITE';
        $cache->HandleCache();
       
        //我的收藏更新
        $cache->method = 'forget';
        $cache->cacheName = 'getFavoriteList';
        $cache->cachePar = array($this->user_id); //集合
        $cache->HandleCache();
    }
    
}